package com.javasm.crm.service.aftersale.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.criteria.repositrory.InOrderCriteria;
import com.javasm.crm.domin.entity.aftersale.Inorder;
import com.javasm.crm.domin.entity.aftersale.Outorder;
import com.javasm.crm.domin.entity.aftersale.RefacInApply;
import com.javasm.crm.domin.entity.base.BaseEntity;
import com.javasm.crm.mapper.aftersale.InorderMapper;
import com.javasm.crm.service.aftersale.IInorderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.yaml.snakeyaml.events.Event;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lirui
 * @since 2021-07-14
 */
@Service
@Transactional
public class InorderServiceImpl extends ServiceImpl<InorderMapper, Inorder> implements IInorderService {

    @Override
    public boolean generateInOutOrder(RefacInApply refacInApply) {

        Inorder inorder = new Inorder();

        inorder.setApplyId(Integer.valueOf(refacInApply.getId().toString()));
        inorder.setApplyCode(refacInApply.getIdoCode());
        inorder.setOutApplyId(refacInApply.getOdoId());
        inorder.setOutApplyCode(refacInApply.getOdoCode());
        inorder.setCreateBy(refacInApply.getCreateBy());
        inorder.setCreateTime(LocalDateTime.now());
        inorder.setStatus(0);//入库状态码：待入库
        inorder.setType(1);//订单类型，返厂入库订单

        boolean save = save(inorder);

        if (!save) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        String inorderCode = UUID.randomUUID().toString().substring(0, 5) + inorder.getId();

        UpdateWrapper<Inorder> updateWrapper = new UpdateWrapper<>();
        LambdaUpdateWrapper<Inorder> lambda = updateWrapper.lambda();

        lambda.eq(BaseEntity::getId, inorder.getId()).set(Inorder::getInorderCode, inorderCode);

        boolean update = update(updateWrapper);

        if (!update) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        return true;
    }

    @Override
    public Page<Inorder> find(InOrderCriteria inOrderCriteria) {

        Integer currentPage = inOrderCriteria.getCurrentPage();
        Integer pageSize = inOrderCriteria.getPageSize();

        QueryWrapper<Inorder> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Inorder> lambda = queryWrapper.lambda();

        String inorderCode = inOrderCriteria.getInorderCode();
        if (inorderCode != null && !"".equals(inorderCode)) {
            lambda.like(Inorder::getInorderCode, inorderCode);
        }

        Integer status = inOrderCriteria.getStatus();
        if (status != null) {
            lambda.eq(Inorder::getStatus, status);
        }

        Integer type = inOrderCriteria.getType();
        if (type != null) {
            lambda.eq(Inorder::getType, type);
        }

        lambda.orderByDesc(BaseEntity::getCreateTime);

        Page<Inorder> page = new Page<>(currentPage, pageSize);
        page(page, queryWrapper);

        return page;
    }
}